package com.amigo.online.provider.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.mapper.UserMapper;
import com.amigo.online.provider.user.model.entity.User;
import com.amigo.online.provider.user.util.ConvertUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserFeignController extends BaseController {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据userId查出用户的信息，供其他服务使用
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{userId}")
    @JsonIgnore
    public ResponseEntity<Response> getUserInfo(@PathVariable Long userId) {
        if (userId == null || userId.intValue() <= 0) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        User user = userMapper.selectByPrimaryKey(userId);
        return success(ConvertUtil.UserToUserInfo(user));
    }
}
